package com.sdy.healthcode.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.constants.Constants;
import com.sdy.healthcode.biz.model.Manager;
import com.sdy.healthcode.biz.service.ManagerService;
import com.sdy.healthcode.web.vo.ManagerVO;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.healthcode.web.controller
 * @date:2020/3/1
 */

@Slf4j
@Controller
@RequestMapping("/getManager")
@Api(tags = "账号管理")
public class ManagerController extends BaseController {
    @Autowired
    private ManagerService managerService;

    @PostMapping("/delbyId")
    @ResponseBody
    @ApiOperation(value = "根据Id删除用户")
    public Response delbyId(HttpServletRequest request, Integer id) throws BizException {
        managerService.delbyId(id);
        return Response.success();
    }

    @PostMapping("/saveOne")
    @ResponseBody
    @ApiOperation(value = "保存用户")
    public Response saveOne(@RequestBody Manager manager) throws BizException {
        Assert.isTrue(!manager.getPhone().matches(Constants.REGEX_PHONE), "手机号格式错误");
        Assert.isBlank(manager.getPassword(), "密码不能为空");
        manager.setState(1);
        Assert.isTrue(managerService.lambdaQuery().eq(Manager::getPhone, manager.getPhone()).count() > 0, "手机号已存在");
        managerService.saveOne(manager);
        return Response.success();
    }

    @PostMapping("/updbyId")
    @ResponseBody
    @ApiOperation(value = "根据Id修改")
    public Response updbyId(@RequestBody Manager manager) {
        managerService.lambdaUpdate()
                .eq(Manager::getId, manager.getId())
                .set(Manager::getName, manager.getName())
                .update();
        return Response.success();
    }

    @PostMapping("/modifyPassword")
    @ResponseBody
    @ApiOperation(value = "修改密码")
    public Response modifyPassword(@RequestBody Manager manager) throws BizException {
        Assert.isBlank(manager.getPassword(), "密码不能为空");
        managerService.lambdaUpdate()
                .eq(Manager::getId, manager.getId())
                .set(Manager::getPassword, manager.getPassword())
                .update();
        return Response.success();
    }

    @PostMapping("/updStaById")
    @ResponseBody
    @ApiOperation(value = "修改用户状态")
    public Response updStaById(HttpServletRequest request, Integer id,Integer state) throws BizException {
        managerService.updStaById(id, state);
        return Response.success();
    }

    @GetMapping("/getPage")
    @ResponseBody
    @ApiOperation(value = "账号分页")
    public Response getPage(HttpServletRequest request,String phone, Page<Manager> page) {
        IPage<Manager> pageResult = managerService.page(page, new LambdaQueryWrapper<Manager>()
                .like(StringUtil.isNotBlank(phone), Manager::getPhone, phone));
        List<ManagerVO> mvList = new ArrayList<>();
        List<Manager> records = pageResult.getRecords();
        for (Manager m : records) {
            ManagerVO managerVO = new ManagerVO();
            managerVO.setId(m.getId());
            managerVO.setName(m.getName());
            managerVO.setPhone(m.getPhone());
            managerVO.setState(m.getState());
            mvList.add(managerVO);
        }
        IPage<ManagerVO> r = new Page<ManagerVO>(pageResult.getCurrent(), page.getSize(), page.getTotal()).setRecords(mvList);
        return Response.success(r);
    }


}
